<div class="modal fade" id="edit-task" tabindex="-1">
    <div class="modal-dialog">
        <form id="edit_task_form" name="edit_task_form" method="post" action="/set/task/save">
            <input type="hidden" name="id" value=""/>
            <input type="hidden" name="status" value="0"/>
            <input type="hidden" name="curren_status" value="0"/>
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h4 class="modal-title" id="batch-title">
                        <if condition="$Think.get.task_type eq 1">
                            编辑加分项信息
                            <input name="task_type" type="hidden" value="1">
                            <else/>
                            编辑任务信息
                            <input name="task_type" type="hidden" value="0">
                        </if>
                        </h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label>标题<span class="text-important">*</span></label>
                        <input name="title" class="form-control" type="text" value="">
                    </div>
                    <div class="form-group" syt>
                        <label>说明<span class="text-important">*</span></label>
                        <textarea name="content" class="form-control" type="text" style="height:120px"></textarea>
                    </div>

                    <div class="row">
                        <div class="form-group col-sm-12">
                            <label>发布单位<span class="text-important">*</span></label>
                            <input name="promulgator" class="form-control" type="text" value="">
                        </div>

                    </div>
                    <div class="row">
                        <div class="form-group  col-sm-4">
                            <label>级别<span class="text-important">*</span></label>
                            <div class="dropdown dropdown-block">
                                <a class="btn btn-default" data-toggle="dropdown"><span name="rank_name" class="option-name">省级</span> <span
                                        class="caret"></span>
                                </a>
                                <ul class="dropdown-menu dropdown-option-menu">
                                    <li><a data-value="1" role="button">省级</a></li>
                                    <li><a data-value="2" role="button">市级</a></li>
                                    <li><a data-value="3" role="button">区级</a></li>
                                </ul>
                                <input name="rank" class="option-value" type="hidden" value="1">
                            </div>
                        </div>
                        <div class="form-group  col-sm-4">
                            <label>属性<span class="text-important">*</span></label>
                            <div class="dropdown dropdown-block">
                                <a class="btn btn-default" data-toggle="dropdown"><span name="attribute_name" class="option-name">全部</span> <span
                                        class="caret"></span>
                                </a>
                                <ul class="dropdown-menu dropdown-option-menu">
                                    <li><a data-value="0" role="button">全部</a></li>
                                    <li><a data-value="1" role="button">单位</a></li>
                                    <li><a data-value="2" role="button">学校</a></li>
                                    <li><a data-value="3" role="button">社区</a></li>
                                </ul>
                                <input name="attribute" class="option-value" type="hidden" value="0">
                            </div>
                        </div>
                        <div class="form-group col-sm-4">
                            <label>总分<span class="text-important">*</span></label>
                            <input name="total_score" class="form-control" type="text" value="">
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="submit" class="btn btn-primary" action-type="draft">保存草稿</button>
                    <button type="button" class="btn btn-primary" action-type="publish">发布任务</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $('#nav-set').addClass("active");
        $(".modal-body").delegate(".deletrow", "click", function () {
            var $this = $(this);
            var $row = $this.parent().parent().parent();
            $row.remove();
        });

        $('[action-type="publish"][type="button"]').click(function () {
            $('#edit_task_form input[name="status"]').val('1');
            $('#edit_task_form').submit();
        });

        $('[data-toggle="modal"][data-target="#edit-task"]').click(function () {
            reset_edit_task_form();
            var $this = $(this);
            var node = $this.parent().parent();

            edit_edit_task_form(node);
        });

        $("#edit_task_form").validate({
            submitHandler: function () {
                $('#edit_task_form button[action-type="draft"]').attr("disabled","true");
                $.post($("#edit_task_form").attr("action"), $("#edit_task_form").serialize(), function (data) {
                    if (data === "0") {
                        alertify.alert("保存成功!", function () {
                            $('#edit_task_form').modal('hide');
                            location.reload();
                        });
                    } else {
                        alertify.alert(data);
                    }
                }).error(function (msg, url, line) {
                    alert(msg.responseText);
                });
            },
            ignore: "",
            rules: {
                title: {
                    required: true
                },
                promulgator: {
                    required: true
                },
                content: {
                    required: true
                },
                total_score: {
                    required: true,
                    digits: true
                }
            },
            messages: {
                title: {
                    required: "请填写标题"
                },
                promulgator: {
                    required: "请填写发布单位"
                },
                content: {
                    required: "请填写内容"
                },
                total_score: {
                    required:  "请填写分值",
                    digits: "请填写正确的分值"
                }
            }
        });

    });

    function reset_edit_task_form() {
        $('#edit_task_form input[name="id"]').val('');
        $('#edit_task_form input[name="status"]').val('0');
        $('#edit_task_form input[name="curren_status"]').val('0');
        $('#edit_task_form input[name="title"]').val('');
        $('#edit_task_form input[name="promulgator"]').val('');
        $('#edit_task_form input[name="total_score"]').val('');
        $('#edit_task_form textarea[name="content"]').val('');
        $('#edit_task_form button[action-type="draft"]').show();
    }

    function edit_edit_task_form(node) {
        $('#edit_task_form input[name="id"]').val(node.data('vid'));
        $('#edit_task_form input[name="status"]').val(node.data('status'));
        $('#edit_task_form input[name="curren_status"]').val(node.data('status'));
        $('#edit_task_form input[name="title"]').val(node.data('title'));
        $('#edit_task_form input[name="promulgator"]').val(node.data('promulgator'));
        $('#edit_task_form input[name="total_score"]').val(node.data('total_score'));
        $('#edit_task_form textarea[name="content"]').val(node.data('content'));

        init_select($('#edit_task_form input[name="rank"]'), $('#edit_task_form span[name="rank_name"]'), node.data('rank'));
        init_select($('#edit_task_form input[name="attribute"]'), $('#edit_task_form span[name="attribute_name"]'), node.data('attribute'));

        if(node.data('status')=='1'){
            $('#edit_task_form button[action-type="draft"]').hide();
        }
    }
    function init_select(ctr, ctr_name, data) {
        ctr.val(data);
        var vx = ctr.val();
//        if (vx != "") {
        ctr.prev().find('a').each(function () {
            var $this = $(this);
            if ($this.data('value') == vx) {
                ctr_name.text($this.text());
            }
        });
//        }
    }
</script>